package com.zsck.pos.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chinaums.commoncheck.R;
import com.zsck.pos.net.NetConfig;

import java.io.ByteArrayOutputStream;

/**
 * 创建者     Bowllboy
 * 创建时间   2020/4/23 16:13
 * 描述	      ${TODO}
 */
public class GlideUtils {
    public static void display(String url, ImageView imageView, Context context){
//        GlideApp.with(context).load(url).dontAnimate().fitCenter().into(imageView);
        Glide.with(context).load(url).dontAnimate().fitCenter().into(imageView);
    }
    public static void disPlayFromOSSWithConner(String url, ImageView imageView, Context context,int conner){
        String imageUrl = NetConfig.IMG_HOST + url;
//        GlideApp.with(context).load(imageUrl).centerCrop().into(imageView);
        displayWithConner(imageUrl,imageView,context,conner);
    }

    /**
     * 加载图片到background
     * @param url
     * @param imageView
     * @param context
     * @param conner
     */
    public static void disPlayFromOSSWithBg(String url, ImageView imageView, Context context,int conner){
        String imageUrl = NetConfig.IMG_HOST + url;
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(conner);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
    /*    GlideApp.with(context)
                .load(imageUrl)
                .apply(options)
                .into(imageView);*/
        Glide.with(context).load(url).apply(options).dontAnimate().fitCenter().into(imageView);
    }


    public static void display(int id, ImageView imageView, Context context){
//        GlideApp.with(context).load(id).centerCrop().into(imageView);
        Glide.with(context).load(id).dontAnimate().fitCenter().into(imageView);
    }
    public static void display(Bitmap bitmap, ImageView imageView, Context context){
//        GlideApp.with(context).load(bitmap).centerCrop().into(imageView);
        Glide.with(context).load(bitmap).dontAnimate().fitCenter().into(imageView);
    }

    public static void displayWithConner(String url, ImageView imageView, Context context,int conner){
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(conner);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
//                .override(300, 300);
        Glide.with(context).load(url).dontAnimate().fitCenter().into(imageView);
//        GlideApp.with(context).load(url).apply(options).into(imageView);
    }
    public static void displayWithConnerDefault(String url, ImageView imageView, Context context,int conner,int defaultid){
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(conner);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.error(defaultid);
        options.placeholder(defaultid);
//                .override(300, 300);
        Glide.with(context).load(url).dontAnimate().fitCenter().into(imageView);
//        GlideApp.with(context).load(url).apply(options).into(imageView);
    }
    public static void displayWithIdConner(int id, ImageView imageView, Context context,int conner){
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(conner);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
//                .override(300, 300);
//        GlideApp.with(context).load(id).apply(options).into(imageView);
        Glide.with(context).load(id).dontAnimate().fitCenter().into(imageView);
    }
    public  static void displayWithCircle(String url, ImageView imageView, Context context){
        RequestOptions options = RequestOptions.circleCropTransform();
//        GlideApp.with(context).load(url).apply(options).into(imageView);
        Glide.with(context).load(url).dontAnimate().fitCenter().into(imageView);
    }
    public static void DipslayBitmapWithCircle(Bitmap bitmap, ImageView imageView, Context context){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes=baos.toByteArray();
        RequestOptions options = RequestOptions.circleCropTransform();
//        GlideApp.with(context).load(bytes).apply(options).into(imageView);
        Glide.with(context).load(bytes).dontAnimate().fitCenter().into(imageView);
    }
}
